package com.fkcai.hansoul.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fkcai.hansoul.common.util.Result;
import com.fkcai.hansoul.model.dto.post.PostCommentDto;
import com.fkcai.hansoul.model.dto.post.PostCommentReplyDto;
import com.fkcai.hansoul.model.vo.post.PostCommentReplyVo;
import com.fkcai.hansoul.model.vo.post.PostCommentVo;
import com.fkcai.hansoul.service.CommentReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论回复控制器
 */
@RestController
@RequestMapping("/api/postComment")
public class CommentReplyController {
	@Autowired
	private CommentReplyService commentReplyService;
	
	/**
	 * 创建帖子评论接口
	 *
	 * @param postCommentDto 评论信息
	 * @return 创建的评论信息
	 */
	@PostMapping("/createPostComment")
	public Result<PostCommentVo> createPostComment (@RequestBody PostCommentDto postCommentDto) {
		PostCommentVo data = commentReplyService.createPostComment(postCommentDto);
		
		return Result.success(data);
	}
	
	
	/**
	 * 根据帖子 id 获取帖子评论接口
	 * @param postId 帖子 id
	 * @param page 页码
	 * @param size 每页数量
	 * @return 帖子评论信息
	 */
	@GetMapping("/getPostCommentByPostId/{postId}")
	public Result<Page<PostCommentVo>>  getPostCommentByPostId (@PathVariable Integer postId,
	                                                     @RequestParam(defaultValue = "1") int page,
	                                                     @RequestParam(defaultValue = "10") int size
	) {
		// 校验页码和每页数量
		page = Math.max(page, 1);
		size = Math.min(Math.max(size, 1), 10);
		
		Page<PostCommentVo> data = commentReplyService.getPostCommentByPostId(postId, page, size);
		
		return Result.success(data);
	}
	
	
	/**
	 * 创建帖子评论回复接口
	 *
	 * @param postCommentReplyDto 回复信息
	 * @return 创建的回复信息
	 */
	@PostMapping("/createPostCommentReply")
	public Result<PostCommentReplyVo> createPostCommentReply (@RequestBody PostCommentReplyDto postCommentReplyDto) {
		PostCommentReplyVo data = commentReplyService.createPostCommentReply(postCommentReplyDto);
		
		return Result.success(data);
	}
	
	
	/**
	 * 根据评论 id 获取帖子评论回复接口
	 * @param commentId 评论 id
	 * @param page 页码
	 * @param size 每页数量
	 * @return 帖子评论回复信息
	 */
	@GetMapping("/getPostCommentReplyByCommentId/{commentId}")
	public Result<Page<PostCommentReplyVo>> getPostCommentReplyByCommentId (@PathVariable Integer commentId,
	                                                                     @RequestParam(defaultValue = "1") int page,
	                                                                     @RequestParam(defaultValue = "10") int size
	){
		// 校验页码和每页数量
		page = Math.max(page, 1);
		size = Math.min(Math.max(size, 1), 10);
		
		Page<PostCommentReplyVo> data = commentReplyService.getPostCommentReplyByPostId(commentId, page, size);
		
		return Result.success(data);
	}
}
